package com.vlong.town.service.house;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vlong.town.entity.house.HouseInfo;
import com.vlong.town.mapper.house.HouseInfoMapper;

/**
 * 房屋出租信息业务实现类
 * @author Assassin
 */
@Service
public class HouseInfoService {

    // Fields

    @Autowired
    private HouseInfoMapper houseInfoMapper;// 房屋出租信息Mapper接口

    // Methods

    /**
     * 添加房屋出租信息
     * @param townId 村镇Id
     * @param title 房屋出租标题
     * @param acreage 房屋面积
     * @param address 房屋位置
     * @param monthlyRent 月租金额
     * @param content 房屋出租内容
     * @param desc 房屋出租简介
     * @param thumbnail 缩略图
     */
    @Transactional
    public void add(Long townId, String title, String acreage, String address, Long monthlyRent, String content, String desc, String thumbnail,String wxId) {
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setTownId(townId);
        houseInfo.setState(1);
        houseInfo.setStatus(1);
        houseInfo.setTitle(title);
        houseInfo.setAcreage(acreage);
        houseInfo.setAddress(address);
        houseInfo.setMonthlyRent(monthlyRent);
        houseInfo.setContent(content);
        houseInfo.setDesc(desc);
        houseInfo.setThumbnail(thumbnail);
        Long cuTime = System.currentTimeMillis();
        houseInfo.setCreateTime(cuTime);
        houseInfo.setUpdateTime(cuTime);
        houseInfo.setPublicUser(wxId);
        this.houseInfoMapper.insert(houseInfo);
    }

    /**
     * 逻辑删除房屋出租信息
     * @param infoId 主键值
     */
    @Transactional
    public void delete(Long infoId) {
        this.houseInfoMapper.delete(infoId);
    }

    /**
     * 修改房屋出租信息
     * @param infoId 房屋出租Id
     * @param title 房屋出租标题
     * @param acreage 房屋面积
     * @param address 房屋位置
     * @param monthlyRent 月租金额
     * @param content 房屋出租内容
     * @param desc 房屋出租简介
     * @param thumbnail 缩略图
     */
    @Transactional
    public void update(Long infoId, String title, String acreage, String address, Long monthlyRent, String content, String desc, String thumbnail) {
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setInfoId(infoId);
        houseInfo.setTitle(title);
        houseInfo.setAcreage(acreage);
        houseInfo.setAddress(address);
        houseInfo.setMonthlyRent(monthlyRent);
        houseInfo.setContent(content);
        houseInfo.setDesc(desc);
        houseInfo.setThumbnail(thumbnail);
        houseInfo.setUpdateTime(System.currentTimeMillis());
        this.houseInfoMapper.update(houseInfo);
    }

    /**
     * 主键查询房屋出租信息
     * @param infoId 房屋出租Id
     * @return
     */
    @Transactional(readOnly = true)
    public HouseInfo queryById(Long infoId) {
        return this.houseInfoMapper.selectById(infoId);
    }

    /**
     * 查询房屋出租信息
     * @param title 房屋出租标题
     * @param content 房屋出租内容
     * @param desc 房屋出租简介
     * @return List<HouseInfo>
     */
    @Transactional(readOnly = true)
    public List<HouseInfo> queryList(String title, String content, String desc,Integer status) {
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setTitle(title);
        houseInfo.setContent(content);
        houseInfo.setDesc(desc);
        houseInfo.setStatus(status);
        return this.houseInfoMapper.selectList(houseInfo);
    }

    /**
     * 审核房屋出租状态
     * @param infoId
     */
	public void updateStatus(Long infoId) {
		 HouseInfo houseInfo = new HouseInfo();
		 houseInfo.setStatus(2);
		 houseInfo.setInfoId(infoId);
		 this.houseInfoMapper.update(houseInfo);
	}

}
